// pages/setup/setup.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    depIndex: 0,
    posIndex: 0,
    depArray: ['口腔内科', '口腔外科', '正畸科', '修复科', '儿童口腔科', '牙粘膜病科'],
    posArray: ['住院医师','主治医师','副主任医师','主任医师'],
    img: "/images/defaultImage.png",
    name: '',
    id: '',
    department: '',
    position: '',
    hosName: '',
    Doctor:[{
      img: '',
      name: '',
      id: '',
      department: '',
      position: '',
      hosName: '',
    }],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getDoctorMessage();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getDoctorMessage();
    this.setData({
      username: getApp().globalData.username,
    })
    try {
      var ap = wx.getStorageSync('Doctor')
      this.setData({
        Doctor: ap
      })
    } catch(e) { }
  },

  /**
   * 医生选择更改个人头像
   */
  chooseImg:function(e){
    var that = this; 
    wx.chooseImage({ 
    count: 1, // 默认9 
    sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有 
    sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有 
    success: function (res) { 
      // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片 
      var tempFilePaths = res.tempFilePaths[0];
      that.setData({ 
        img: tempFilePaths 
      }) 
      console.log(that.data.img)
      console.log('正在选择图片')
      that.upLoadPhoto(tempFilePaths)
    } 
    }) 
  },

   /**
   * 上传更改后的个人头像
   */
    upLoadPhoto: function (options) {
    var filePath1 = options
    var that = this
    console.log('正在上传头像')
    wx.uploadFile({
      url: getApp().globalData.url + 'setImgServlet', //里面填写你的上传图片服务器API接口的路径 
      filePath: filePath1, //要上传文件资源的路径 String类型 
      name: 'avatar100', //按个人情况修改，文件对应的 key,开发者在服务器端通过这个 key 可以获取到文件二进制内容，(后台接口规定的关于图片的请求参数)
      header: {
        "Content-Type": "multipart/form-data" //记得设置
      },
      formData: {
        tel: getApp().globalData.username
      },
      success: function (res) {
        console.log(res.data)
        if( res.data == 'False')
        {
          wx.showToast({
            title: '上传照片失败，请重新选择照片',
            icon: 'none',
            duration: 2000
          })
        }
        else 
        {
          that.getDoctorMessage()
        }
      },
      fail(res) {
        wx.showToast({
          title: '网络连接失败，请从顶部下拉刷新',
          icon: 'none',
          duration: 2000
        })
      }
    })
  }, 

  /** 
   * 医生输入姓名
  */
  nameInput: function(e){
    this.setData({
      name: e.detail.value
    })
  },

   /**
   * 医生选择科室
   */
  depBindPickerChange: function(e){
    this.setData({
      depIndex: e.detail.value,
    })
    this.setData({
      department: this.data.depArray[this.data.depIndex],
    })
  },

  /**
   * 医生选择职称
   */
  posBindPickerChange: function(e){
    this.setData({
      posIndex: e.detail.value,
    })
    this.setData({
      position: this.data.posArray[this.data.posIndex],
    })
  },

  /**
   * 获取医生信息
   */
  getDoctorMessage: function () {
    var that = this
    wx.request({
      url: getApp().globalData.url + 'getDoctorInfoServlet',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        tel: getApp().globalData.username,
      },
      success: function (res) {
        console.log(res.data)
        if (res.data != false && res.data != "false"&& res.data != "") {
          try {
            wx.setStorageSync("Doctor", res.data)
          } catch (e) { }
          that.setData({
            Doctor: res.data,
          })
          that.setData({
            img: that.data.Doctor.img,
            name: that.data.Doctor.name,
            id: that.data.Doctor.id,
            department: that.data.Doctor.department,
            position: that.data.Doctor.position,
            hosName: that.data.Doctor.hosName,
          })
          console.log(that.data.Doctor)
        }
      },
      fail(res) {
        wx.showToast({
          title: '获取失败，请从顶部下拉刷新',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

  /**
   * 医生确认保存个人信息
   */
  submitBtnClick: function(){
    if (this.data.name == "" || this.data.name == undefined) 
    {
      wx.showToast({
        title: '请输入您的姓名',
        icon: 'none'
      })
      return;
    } 
    if (this.data.department == "" || this.data.department == undefined) 
    {
      wx.showToast({
        title: '请选择您的科室',
        icon: 'none'
      })
      return;
    } 
    if (this.data.position == "" || this.data.position == undefined) 
    {
      wx.showToast({
        title: '请输入您的职称',
        icon: 'none'
      })
      return;
    } 
    else
    {
      console.log(this.data.name)
      var that = this
      wx.request({
        url: getApp().globalData.url + 'setupServlet',
        method: 'GET',
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        data: {
          tel: getApp().globalData.username,
          name: this.data.name,
          department: this.data.department,
          position: this.data.position,
        },
        success(res) {
          if( res.data == 'False')
          {
            wx.showToast({
              title: '上传个人信息失败，请重新更改',
              icon: 'none',
              duration: 2000
            })
          }
          else 
          {
            wx.showToast({
              title: '修改个人信息成功',
              icon: 'none',
              duration: 2000
            })
            that.getDoctorMessage()
          }
        },
        fail(res) {
          wx.showToast({
            title: '网络连接失败',
            icon: 'none',
            duration: 2000
          })
        }
      })
    }
  }
})